package com.hetao.lazy;

/**
 * Created by hetao on 2018/3/12.
 * 懒汉式单例
 * 只有在外部类被调用的时候内部类才会被加载
 * 兼顾了饿汉式的内存浪费，也兼顾了synchronized性能问题
 */
public class LazyThree {
    private static boolean initialized=false;
    private LazyThree(){
        synchronized (LazyThree.class){
            if(!initialized){
                initialized=!initialized;
            }else{
                throw new RuntimeException("单例已被侵犯");
            }
        }
    }
    public static final LazyThree getInstance(){
        //调用方法之间必须加载内部类
        return LazyHolder.LAZY;
    }

    private static class LazyHolder{
        private static final LazyThree LAZY=new LazyThree();

    }
}
